package com.touchapk.opencv;

import java.awt.image.BufferedImage;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;

public class DetectUtility {
	
	final static FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT);
	final static DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
	final static DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);

	/**
	 * 
	* @author yangkeyun 
	* @description 提取特征点
	* @param image
	* @param keypoints
	* @param descriptors
	 */
	static void analyze(Mat image, MatOfKeyPoint keypoints, Mat descriptors) {
		detector.detect(image, keypoints);
		extractor.compute(image, keypoints, descriptors);
	}

	/**
	 * 
	* @author yangkeyun 
	* @description 模版匹配
	* @param desc1
	* @param desc2
	* @param matches
	 */
	static void match(Mat desc1, Mat desc2,MatOfDMatch matches ) {
		
		matcher.match(desc1, desc2, matches);
		
	}
	
	
	/**
	 * 
	 * @author yangkeyun
	 * @description BufferedImage 转换 mat
	 * @param in
	 * @return
	 */
	static Mat img2Mat(BufferedImage in){

		Mat mat = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC3);

		byte[] data= new byte[in.getHeight() * in.getWidth()* (int) mat.elemSize()];

		int[] dataBuff = in.getRGB(0, 0, in.getWidth(), in.getHeight(),null, 0, in.getWidth());
		
		if (in.getType() == BufferedImage.TYPE_INT_RGB) {
			
			for (int i = 0; i < dataBuff.length; i++) {
				data[i * 3] = (byte) ((dataBuff[i] >> 16) & 0xFF);
				data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
				data[i * 3 + 2] = (byte) ((dataBuff[i] >> 0) & 0xFF);
			}
			
		}  else  if(in.getType() == BufferedImage.TYPE_4BYTE_ABGR
				||in.getType()==BufferedImage.TYPE_3BYTE_BGR
				||in.getType()==BufferedImage.TYPE_4BYTE_ABGR_PRE){
			
			for (int i = 0; i < dataBuff.length; i++) {
				data[i * 3] = (byte) ((dataBuff[i] >> 0) & 0xFF);
				data[i * 3 + 1] = (byte) ((dataBuff[i] >> 8) & 0xFF);
				data[i * 3 + 2] = (byte) ((dataBuff[i] >> 16) & 0xFF);
			}
			
		}else {
			
			mat = new Mat(in.getHeight(), in.getWidth(), CvType.CV_8UC1);
			int r, g, b;
			for (int i = 0; i < dataBuff.length; i++) {
			
				r = (byte) ((dataBuff[i] >> 16) & 0xFF);

				g = (byte) ((dataBuff[i] >> 8) & 0xFF);

				b = (byte) ((dataBuff[i] >> 0) & 0xFF);
				
				data[i] = (byte) ((0.22 * r) + (0.71 * g) + (0.07 * b));
			}
		}
		
		mat.put(0, 0, data);

		return mat;
	}
	
	/**
	 * 
	* @author yangkeyun 
	* @description
	* @param bgr
	* @return
	 */
	static int[] c16To10(String bgr){
		
		int [] color=new int[]{-1,-1,-1};
		
		if((bgr!=null&&!"".equals(bgr))&&bgr.startsWith("#")&&(bgr.length()==7||bgr.length()==4)){
		
			if(bgr.length()==7){
				try{
					color[0]=Integer.parseInt(bgr.substring(1,3),16);
					color[1]=Integer.parseInt(bgr.substring(3,5),16);
					color[2]=Integer.parseInt(bgr.substring(5,7),16);
				}catch(Exception e){}
			}else{
				try{
					String b=bgr.substring(1,2);
					b+=b;
					String g=bgr.substring(2,3);
					g+=g;
					String r=bgr.substring(3,4);
					r+=r;
					color[0]=Integer.parseInt(b,16);
					color[1]=Integer.parseInt(g,16);
					color[2]=Integer.parseInt(r,16);
					
				}catch(Exception e){}
			}
		}
		return color;
	}
	
}
